package cn.hotel.mapper;

import cn.hotel.utlis.DataSourceConfig;

import java.sql.*;
import java.util.List;

/**
 * @authorDesc 诸神在上，佑我程序，玄功护体，BUG皆去！
 * @author ZhouJl
 * @date 2022-12-26 10:16:49
 * @version 1.0.0
 * @description JDBC封装类
 */
public class BaseDao {
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;
    private static DataSourceConfig dataSourceConfig = DataSourceConfig.getInstance();
    private static final String driver = dataSourceConfig.getString("jdbc.CONNECT");
    private static final String url = dataSourceConfig.getString("jdbc.URL");
    private static final String user = dataSourceConfig.getString("jdbc.USER");
    private static final String password = dataSourceConfig.getString("jdbc.PASSWORD");

    /**
     * 创建数据库连接
     * @return
     */
    public Connection getConn(){
        try {
            //加载驱动
            Class.forName(driver);
            //建立数据库连接
            connection = DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

    /**
     * 针对增删改的封装方法
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql,List<Object> params){
        //先获取连接
        getConn();
        int result = 0;
        try {
            //创建preparedStatement进行预编译SQL
            preparedStatement = connection.prepareStatement(sql);
            //传参
            if(params !=null && params.size() > 0){
                for (int i = 0;i < params.size();i++) {
                    preparedStatement.setObject((i+1),params.get(i));
                }
            }
            //执行SQL并返回结果
            result = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            closeSource(null);
        }
        return result;
    }

    /**
     * 针对查询的封装方法
     * @param sql
     * @param params
     * @return
     */
    public ResultSet executeQuery(String sql, List<Object> params){
        //先获取连接
        getConn();
        //创建preparedStatement进行预编译SQL
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            //传参
            if(params !=null && params.size() > 0){
                for (int i = 0;i < params.size();i++) {
                    preparedStatement.setObject((i+1),params.get(i));
                }
            }
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 关闭资源
     * @param resultSet
     */
    public void closeSource(ResultSet resultSet){
        //释放资源
        try {
            if (resultSet!=null) {
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}